﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Profiles;
    using System.ComponentModel;
    using System.Web.Security;

    public partial class GroupThumbAvatar : System.Web.UI.UserControl
    {
        Guid _groupId;
        bool _uidSet = false;
        public Guid GroupId 
        {
            get
            {
                return _groupId;
            }
            set
            {
                _groupId = value;
                _uidSet = true;
            }
        }

        string _cssClass = string.Empty;
        bool _showName = true;
        bool _showOnlineStatus = true;
        bool _addHash = true;

        public bool ShowOnlineStatus { get { return _showOnlineStatus; } set { _showOnlineStatus = value; } }
        public bool ShowGroupName { get { return _showName; } set { _showName = value; } }
        public string CssClass { get { return _cssClass; } set { _cssClass = value; } }
        public bool AddHash { set { _addHash = value; } get { return _addHash; } }

        public GroupThumbAvatar()
        {
            //this.Load += new EventHandler(ThumbAvatar_Load);
            //this.PreRender += new EventHandler(ThumbAvatar_PreRender);
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!_uidSet)
            {
                throw new Exception("GroupId must be set");
            }

            pnNickName.Visible = _showName;

            if (_cssClass != string.Empty)
            {
                pn1.CssClass += " " + _cssClass;
            }

            try
            {
                using (scDataContext cxt = new scDataContext())
                {
                    Sc_Group p = cxt.Sc_Groups.Single(d => d.GroupId == _groupId);

                    string avatarmagePath = string.Empty;

                    if (p.AvatarImage == null)
                    {
                        avatarmagePath = WebConsts.G_AVATAR_DEFAULT_IMG_THUMBNAIL_PATH;
                        var format = _addHash ? "{0}?{1}" : "{0}";
                        lnkImage.ImageUrl = string.Format(format, avatarmagePath, DateTime.Now.Ticks);
                    }
                    else
                    {
                        avatarmagePath = p.AvatarImage.Element("Thumbnail").Value;
                        var format = AddHash ? "{0}/{1}?{2}" : "{0}/{1}";
                        lnkImage.ImageUrl = string.Format(format, WebConsts.AVATAR_FOLDER, avatarmagePath, DateTime.Now.Ticks);
                    }

                    lnkImage.ToolTip = p.GroupName;
                }
            }
            catch { }

            lnkImage.NavigateUrl = string.Format("~/Mod/Groups/ViewGroup.aspx?id={0}", _groupId);
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}